﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SCRUM.Core.BusinessObjects;
using System.Collections.Generic;
using SCRUM.Core.DAO;

/// <summary>
/// StateSelection is used to pick one of the states of item, which is of spedified type
/// </summary>
public partial class StateSelection : ObjectSelection
{
    protected override WebControl GetWebControl()
    {
        return this.DropDownListStates;
    }

    protected override Label GetLabel()
    {
        return this.FieldLabel;
    }

    /// <summary>
    /// Handler friggered when selected state changes
    /// </summary>
    public event EventHandler SelectedStateChangedEvent;
    //public bool isFilter;// = false;

    /// <summary>
    /// Enables/disables autopostback for this control
    /// </summary>
    public bool AutoPostBack
    {
        get { return this.DropDownListStates.AutoPostBack; }
        set { this.DropDownListStates.AutoPostBack = value; }
    }

    /// <summary>
    /// List of all states available
    /// </summary>
    private IList<State> states;

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        this.FieldLabel.Text = this.LabelText;
        states = DAOState.GetAllStatesForType(this.Type);
        DropDownListStates.DataSource = states;
        DropDownListStates.DataBind();
        if (IsFilter)
        {
            DropDownListStates.Items.Insert(0, TopListItemForFilter(this.Type.ToString() + "State"));
        }
        
    }

    /// <summary>
    /// Gets or sets the type of state
    /// </summary>
    /// <value>The type.</value>
    public State.StateTypes Type { get; set; }

    /// <summary>
    /// Gets or sets the selected state
    /// </summary>
    /// <value>The state of the selected.</value>
    public State SelectedState
    {
        get
        {
            int selectedIndex = DropDownListStates.SelectedIndex;
            selectedIndex = this.IsFilter ? selectedIndex - 1 : selectedIndex;
            return selectedIndex < 0 ? null : states[selectedIndex];
            //return new State(Int32.Parse(DropDownListStates.SelectedItem.Value), DropDownListStates.SelectedItem.Text, this.Type);
        }
        set
        {
            //this.DropDownListStates.SelectedIndex = -1;
            DropDownListStates.SelectedValue = value.ID.ToString();
        }
    }

    /// <summary>
    /// Gets the value of selected state ID
    /// </summary>
    public int SelectedStateID 
    { 
        get 
        {
            State state = this.SelectedState;
            return state == null ? -1 : state.ID;
        } 
    }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="ScrumInput"/> is editable.
    /// </summary>
    /// <value><c>true</c> if editable; otherwise, <c>false</c>.</value>
    public bool Enabled
    {
        get
        {
            return this.DropDownListStates.Enabled;
        }
        set
        {
            this.DropDownListStates.Enabled = value;
        }
    }

    /// <summary>
    /// Gets or sets the index of the selected.
    /// </summary>
    /// <value>The index of the selected.</value>
    public int SelectedIndex
    {
        get { return this.DropDownListStates.SelectedIndex; }
        set { DropDownListStates.SelectedIndex = value; }
    }

    /// <summary>
    /// Selected index changed -> sends event
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedStateChanged(object sender, EventArgs e)
    {
        if (this.SelectedStateChangedEvent != null)
        {
            this.SelectedStateChangedEvent(this, e);
        }
    }
}
